package com.lyhengtongwl.zqsnews.ui.fragment.goodsdetail;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.*;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.flyco.tablayout.SlidingTabLayout;
import com.lyhengtongwl.zqsnews.App;
import com.lyhengtongwl.zqsnews.R;
import com.lyhengtongwl.zqsnews.base.ViewPagerFragment;
import com.lyhengtongwl.zqsnews.utils.SPUtils;
import com.lyhengtongwl.zqsnews.widget.DetailWebView;


/**
 * Created by Steven on 2019/1/15.
 */
@SuppressLint("ValidFragment")
public class DetailFragment extends ViewPagerFragment {

    int topHeight = 0;//顶部的距离
    LinearLayout rootLayout;//跟布局
    private RelativeLayout headerRel;
    private ImageView backImg;
    private ImageView shareImg;
    private ImageView backImg1;
    private ImageView shareImg1;
    private SlidingTabLayout slidingTabLayout;
    private View view;
    private DetailWebView webView;
    private String goodId;

    public DetailFragment(String goodId, int titleHeight, RelativeLayout headerRel, SlidingTabLayout slidingTabLayout, ImageView backImg, ImageView shareImg, ImageView backImg1, ImageView shareImg1) {
        this.goodId = goodId;
        this.topHeight = titleHeight;
        this.headerRel = headerRel;
        this.slidingTabLayout = slidingTabLayout;
        this.backImg = backImg;
        this.shareImg = shareImg;
        this.backImg1 = backImg1;
        this.shareImg1 = shareImg1;
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_detail, null);
        webView = view.findViewById(R.id.webView);
        setWebView();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        //设置跟布局的padding值
        rootLayout = view.findViewById(R.id.ll_rootlayout);
        rootLayout.setPadding(0, topHeight, 0, 0);
    }


    @Override
    protected void onFirstUserVisible() {
        setTitleVisible();
    }

    @Override
    protected void onUserVisible() {
        setTitleVisible();
    }

    private void setTitleVisible() {
        //可见时，标题栏容器不透明,隐藏按钮标题
        headerRel.getBackground().setAlpha(255);
        headerRel.findViewById(R.id.rl_titlebar).setVisibility(View.VISIBLE);
        headerRel.findViewById(R.id.rl_iconbar).setVisibility(View.GONE);
        backImg.setImageAlpha(255);
        shareImg.setImageAlpha(255);
        slidingTabLayout.setTextSelectColor(getAlphaColorSelect());
        slidingTabLayout.setTextUnselectColor(getAlphaColorUnSelect());
        slidingTabLayout.setIndicatorColor(getAlphaIndicatorColorSelect());
    }

    @Override
    protected void onUserInvisible() {

    }


    private int getAlphaColorSelect() {
        return Color.argb((int) (1.0f * 255), 0x33, 0x33, 0x33);
    }

    private int getAlphaColorUnSelect() {
        return Color.argb((int) (1.0f * 255), 0x66, 0x66, 0x66);
    }

    private int getAlphaIndicatorColorSelect() {
        return Color.argb((int) (1.0f * 255), 0xFF, 0x66, 0x00);
    }

    //设置webView
    @SuppressLint({"SetJavaScriptEnabled", "ClickableViewAccessibility"})
    private void setWebView() {
        String url = SPUtils.get(App.getContext(), "detail_url", "") + "";
        final WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setAppCacheMaxSize(1024 * 1024 * 8);
        String appCachePath = App.getContext().getCacheDir().getAbsolutePath();
        settings.setAppCachePath(appCachePath);
        settings.setAllowFileAccess(true);
        settings.setAppCacheEnabled(true);
        //解除数据阻止
        settings.setBlockNetworkImage(false);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            settings.setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
            settings.setMixedContentMode(2);
        }
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                //网页在webView中打开
                if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP) {//安卓5.0的加载方法
                    view.loadUrl(request.toString());
                } else {//5.0以上的加载方法
                    view.loadUrl(request.getUrl().toString());
                }
                return true;
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                Log.e("Tag", "onReceivedError: ");
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();  // 接受所有网站的证书
            }

            //
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
            }

            @Override
            public void onPageFinished(WebView view, String url) {
            }
        });

        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
            }
        });
    }

}
